package com.hbgc.controller;

import com.github.pagehelper.PageInfo;
import com.hbgc.common.PageResult;
import com.hbgc.pojo.TbType;
import com.hbgc.pojo.TbUser;
import com.hbgc.service.IUserService;
import com.hbgc.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private IUserService iUserService;




    /**
     * 保存数据
     * @param tbUser
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public PageResult save(TbUser tbUser)
    {
        boolean status=iUserService.saveTbbUser(tbUser);
        if(status)
        {
            return new PageResult(0,"保存成功");
        }
        else {
            return new PageResult(1,"保存失败");
        }
    }


    /**
     * 修改数据
     * @param tbUser
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public  PageResult update(TbUser tbUser)
    {

        boolean status=iUserService.updateTbbUserById(tbUser);
        if(status)
        {
            return new PageResult(0,"修改成功");
        }
        else {
            return new PageResult(1,"修改失败");
        }
    }

    /**
     * 删除单个
     * @param uid
     * @return
     */
    @RequestMapping("/remove/{uid}")
    @ResponseBody
    public  PageResult remove(@PathVariable long uid)
    {

        boolean status=iUserService.deleteTbUserById(uid);
        if(status)
        {
            return new PageResult(0,"删除成功");
        }
        else {
            return new PageResult(1,"删除失败");
        }
    }


    /**
     * 查询单个
     * @param uid
     * @return
     */
    @RequestMapping("/querybyid/{uid}")
    @ResponseBody
    public  PageResult querybyid(@PathVariable long uid)
    {

        TbUser tbUser=iUserService.getTbbUserById(uid);
        if(tbUser!=null)
        {
            PageResult pageResult=new PageResult(0,"查询成功");
            List data=new ArrayList();
            data.add(tbUser);
            pageResult.setData(data);
            return pageResult;
        }
        else {
            return new PageResult(1,"查询失败");
        }
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param typename
     * @return
     */
    @RequestMapping("/queryByPage")
    @ResponseBody
    public PageResult queryByPage(@RequestParam(required = false,defaultValue = "1") int page, @RequestParam(required = false,defaultValue = "10") int limit, @RequestParam(required = false,defaultValue = "") String username)
    {
        PageInfo pageInfo=iUserService.queryTbbUserByPage(page,limit,username);
        return ConvertUtils.convertData(pageInfo);
    }


}
